package com.study.service; 

import java.util.List;
import javax.annotation.Resource;
import com.study.dao.StaffDao;
import com.study.entity.Staff;
import com.study.util.Tools;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;  
import com.study.util.MulFileUpload;
import org.springframework.web.multipart.MultipartFile;
import com.study.util.Constant;
/**  
 *  
 */  
@Transactional
@Service
public class StaffService  
{  
    /********** 注入StaffDao ***********/  
    @Resource(name="staffDao")
	private StaffDao staffDao;
	

	//返回的json数据的key
	private final static String RE_CODE_STR = "code";
	private final static String RE_DATA_STR = "data";
	private final static String RE_PAGE_STR = "pages";//数据共有多少页
	private final static String RE_COUNT_STR = "count";//数据的总数
	
	//返回的json数据的value
	//返回 0 表示操作成功
	private final static int RE_CODE_0 = 0;
	//返回 1表示操作失败
	private final static int RE_CODE_1 = 1;
	//返回 2表示没有数据
	private final static int RE_CODE_2 = 2;
    
    //增加一个Staff
    public String saveStaff(Staff staff, MulFileUpload files){
		JSONObject resultObj = new JSONObject();
		//上传文件
		List<MultipartFile> list = files.getFiles();
    	if(list != null && !list.isEmpty()){
		 staff.setPhoto("/img/"+MulFileUpload.savePic(list.get(0)));
    	}
		staffDao.saveStaff(staff);
		
		resultObj.put(RE_CODE_STR, RE_CODE_0);
		return resultObj.toString();
		
	}
	
	
	//根据id查询Staff
    public String getStaffById(String id){
    	JSONObject resultObj = new JSONObject();
        
        Staff result = staffDao.getStaffById(id);
        if(result==null){
            resultObj.put(RE_CODE_STR, RE_CODE_2);
        }else{
            resultObj.put(RE_CODE_STR, RE_CODE_0);
            JSONObject jb = new JSONObject(result);
            resultObj.put(RE_DATA_STR, jb);
        }
        
        return resultObj.toString();
    }
	
	
	
	
	
	//注册一个User
    public String registerStaff(Staff staff){
        JSONObject resultObj = new JSONObject();
        
        List<Staff> list = staffDao.listStaff(staff,null,null);
        if(list==null || list.size()==0){
            staffDao.saveStaff(staff);
            resultObj.put(RE_CODE_STR, RE_CODE_0);
        }else{
            resultObj.put(RE_CODE_STR, RE_CODE_1);
            resultObj.put(RE_DATA_STR, "该用户名已存在");
        }
        
        return resultObj.toString();
    }
    
    
    //修改密码
    public String modifyPass(String userid, String oripass, String newpass){
    	JSONObject resultObj = new JSONObject();
        Staff staff = staffDao.getStaffById(userid);
        String pass = staff.getPassword();
        if (pass.equals(oripass)) {
        	staffDao.updatePasswordInStaff(userid,newpass);
        	resultObj.put(RE_CODE_STR, RE_CODE_0);
        } else {
        	resultObj.put(RE_CODE_STR, RE_CODE_1);
            resultObj.put(RE_DATA_STR, "原始密码不正确");
        }
        return resultObj.toString();
    }
	
	//分页条件查询Staff
	public String listStaff(Staff staff, String page, String number){
		JSONObject resultObj = new JSONObject();
		String from = null;
		if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			from = String.valueOf((Integer.parseInt(page)-1)*Integer.parseInt(number));
		}
		
	    List<Staff> listPage = staffDao.listStaff(staff,from, number);
	    if (listPage.isEmpty()) {
			resultObj.put(RE_CODE_STR, RE_CODE_2);
		} else {
			JSONArray array = new JSONArray();
			for(int i=0,len=listPage.size();i<len;i++){
				Staff item = listPage.get(i);
				array.put(i, new JSONObject(item));
			}
		
	        resultObj.put(RE_CODE_STR, RE_CODE_0);
			resultObj.put(RE_DATA_STR, array);
			if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			    long count = staffDao.getStaffCount(staff);
				resultObj.put(RE_PAGE_STR, Math.ceil(count/Double.parseDouble(number)));
				resultObj.put(RE_COUNT_STR, count);
			}
		}
		return resultObj.toString();
	}
	

		
	//根据id删除Staff
	public String removeStaffById(String id){
		JSONObject resultObj = new JSONObject();
		boolean res =  staffDao.removeStaffById(id);
		if (res) {
			resultObj.put(RE_CODE_STR, RE_CODE_0);
		} else {
			resultObj.put(RE_CODE_STR, RE_CODE_1);
		}
		return resultObj.toString();
	}
	
	//更新Staff
	public String updateStaff(Staff staff, MulFileUpload files){
		JSONObject resultObj = new JSONObject();
		
		//上传文件
		List<MultipartFile> list = files.getFiles();
    	if(list != null && !list.isEmpty()){
		staff.setPhoto(Constant.UPLOAD_URL+MulFileUpload.savePic(list.get(0)));
    	}
	    staffDao.updateStaff(staff);
	    resultObj.put(RE_CODE_STR, RE_CODE_0);
	    return resultObj.toString();
	}
	
	//更新Staff表中的password
	public String updatePasswordInStaff(String id, String password){
		JSONObject resultObj = new JSONObject();
		boolean res =  staffDao.updatePasswordInStaff(id, password);
		if (res) {
			resultObj.put(RE_CODE_STR, RE_CODE_0);
		} else {
			resultObj.put(RE_CODE_STR, RE_CODE_1);
		}
		return resultObj.toString();
	}
	
    
}